<?php
namespace app\wx_api\model;

use app\common\model\Model;

class UserModel extends Model {

    /**
     * 保存或者更新微信用户详情
     *
     * @param $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function saveUser($data) {
        $user = $this->where(['open_id' => $data['open_id']])->find();

        if (empty($user)) {
            $data['created_time'] = date('Y-m-d H:i:s');
            if (!$this->save($data)) {
                return false;
            }
        } else { // 更新用户数据
            $this->save($data, ['open_id' => $data['open_id']]);
        }
        return true;
    }

}